package com.qf.smsplatform.strategy.utils;

import com.qf.smsplatform.common.constants.RabbitMQConstants;
import com.qf.smsplatform.common.constants.ReportStateConstants;
import com.qf.smsplatform.common.constants.SourceType;
import com.qf.smsplatform.common.model.StandardReport;
import com.qf.smsplatform.common.model.StandardSubmit;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @Author: LeeRui
 * 2021/1/28 上午 1:47
 **/
@Component
public class PushMsgUtil {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //校验失败发送消息到MQ写日志队列中，并将standardSubmit存到ES中
    public void sendLog(StandardSubmit standardSubmit, String errorCode) {
        // 将errorCode封装的到submit
        standardSubmit.setErrorCode(errorCode);

        //在策略模块发送了写日志消息说明发送失败,状态设置为2
        standardSubmit.setReportState(ReportStateConstants.FAIL);

        //将standardSubmit作为消息发送到SendLog的交换机中
        rabbitTemplate.convertAndSend(RabbitMQConstants.TOPIC_SMS_SEND_LOG + "_exchange", "suibianxie", standardSubmit);
    }

    //校验失败发送消息到MQ给用户下发报告的队列中，并将standardReport存到ES中
    public void pushReport(StandardSubmit standardSubmit, String errorCode) {
        //判断发送短信方式是否未HTTP
        if (standardSubmit.getSource() == SourceType.HTTP) {
            //封装standardReport对象
            StandardReport standardReport = new StandardReport();
            standardReport.setClientID(standardSubmit.getClientID());
            standardReport.setState(ReportStateConstants.FAIL);
            standardReport.setSrcID(standardSubmit.getSrcSequenceId());
            standardReport.setErrorCode(errorCode);
            standardReport.setMobile(standardSubmit.getDestMobile());
            standardReport.setSequenceId(standardSubmit.getSequenceId());

            //将standardReport作为消息发送到PushSmsReport队列
            rabbitTemplate.convertAndSend(RabbitMQConstants.TOPIC_PUSH_SMS_REPORT + "_exchange", "suibianxie", standardReport);
        }
    }
}
